//
//  LDY_ReadViewController.m
//  ProjectOfBStage
//
//  Created by dlios on 15/11/13.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "LDY_ReadViewController.h"
#import "Network.h"
#import "ReadingModel.h"
#import "LDY_ReadCollectionViewCell.h"
#import "SDCycleScrollView.h"
#import "LDY_CycleViewController.h"
#import "LDY_ArticleDetailViewController.h"
#import <MJRefresh.h>

#define KWIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define KHEIGHT ([UIScreen mainScreen].bounds.size.height/667)

@interface LDY_ReadViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, SDCycleScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataArr;

@property (nonatomic, retain) NSMutableArray *mainArr;

@property (nonatomic, retain) NSMutableArray *cycleArr;

@property (nonatomic, retain) NSMutableArray *cycleUrlArr;

@property (nonatomic, retain) NSMutableArray *cycleImgArr;

@property (nonatomic, retain) UICollectionView *readCollectionView;

@property (nonatomic, retain) SDCycleScrollView *cycleScrollView;


@end

@implementation LDY_ReadViewController

- (void)dealloc
{
    [_dataArr release];
    [_mainArr release];
    [_cycleArr release];
    [_cycleUrlArr release];
    [_cycleImgArr release];
    [_readCollectionView release];
    [_cycleScrollView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataArr = [NSMutableArray array];
        
        self.mainArr = [NSMutableArray array];
        
        self.cycleArr = [NSMutableArray array];
        
        self.cycleUrlArr = [NSMutableArray array];
        
        self.cycleImgArr = [NSMutableArray array];
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self dataHandle];
    
    [self creatCollectionView];
}


- (void)dataHandle
{
    [Network networkPOSTRequestWithURL:@"http://api2.pianke.me/read/columns" body:@"auth=&client=1&deviceid=7F95D47F-BF62-4528-B18E-EE921FFC5323&version=3.0.6" pageUniquenessIdentifier:@"auth=&client=1&deviceid=7F95D47F-BF62-4528-B18E-EE921FFC5323&version=3.0.6" result:^(id result) {
        
        NSDictionary *dic = result;
        
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        
        for (NSDictionary *dic in [dataDic objectForKey:@"carousel"]) {
            
            [self.cycleArr addObject:dic];
            
        }
        
        for (NSDictionary *dic in self.cycleArr) {
            
            NSString *img = [dic objectForKey:@"img"];
            
            NSString *url = [dic objectForKey:@"url"];
            
            [self.cycleImgArr addObject:img];
            [self.cycleUrlArr addObject:url];
        }
        
        /* 轮播图图片数组赋值 */
        
        self.cycleScrollView.imageURLStringsGroup = self.cycleImgArr;
        
        self.dataArr = [dataDic objectForKey:@"list"];
        
//        NSLog(@"%@", dataDic);
        
        for (NSDictionary *dic in self.dataArr) {
            
            ReadingModel *model = [ReadingModel modelWithDictionary:dic];
            
            [self.mainArr addObject:model];
        }
        
        if (self.mainArr.count > 0) {
            
            [self.readCollectionView reloadData];
        }
        
    }];
}


- (void)headerRereshing
{    
    [self.mainArr removeAllObjects];
    
    [self.cycleArr removeAllObjects];
    
    [self.cycleImgArr removeAllObjects];
    
    [self.cycleUrlArr removeAllObjects];
    
    [self dataHandle];
    
//    [Network networkPOSTRequestWithURL:@"http://api2.pianke.me/read/columns" body:@"auth=&client=1&deviceid=7F95D47F-BF62-4528-B18E-EE921FFC5323&version=3.0.6" pageUniquenessIdentifier:@"auth=&client=1&deviceid=7F95D47F-BF62-4528-B18E-EE921FFC5323&version=3.0.6" result:^(id result) {
//        
//        NSDictionary *dic = result;
//        
//        NSDictionary *dataDic = [dic objectForKey:@"data"];
//        
//        for (NSDictionary *dic in [dataDic objectForKey:@"carousel"]) {
//            
//            [self.cycleArr addObject:dic];
//            
//        }
//        
//        for (NSDictionary *dic in self.cycleArr) {
//            
//            NSString *img = [dic objectForKey:@"img"];
//            
//            NSString *url = [dic objectForKey:@"url"];
//            
//            [self.cycleImgArr addObject:img];
//            [self.cycleUrlArr addObject:url];
//        }
//        
//        /* 轮播图图片数组赋值 */
//        
//        self.cycleScrollView.imageURLStringsGroup = self.cycleImgArr;
//        
//        self.dataArr = [dataDic objectForKey:@"list"];
//        
//        //        NSLog(@"%@", dataDic);
//        
//        for (NSDictionary *dic in self.dataArr) {
//            
//            ReadingModel *model = [ReadingModel modelWithDictionary:dic];
//            
//            [self.mainArr addObject:model];
//        }
//                
////        if (self.mainArr.count > 0) {
////            
////            [self.readCollectionView reloadData];
////        }
//        
//    }];
    
    [self.readCollectionView.header endRefreshing];
    
    
}


- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
//    flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
//    flowLayout.minimumLineSpacing = 10;
//    flowLayout.minimumInteritemSpacing = 10;
    
    
    self.readCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH * 375, KHEIGHT * 667 - 64) collectionViewLayout:flowLayout];
    [self.view addSubview:self.readCollectionView];
    
    self.readCollectionView.delegate = self;
    self.readCollectionView.dataSource = self;
    
    self.readCollectionView.backgroundColor = [UIColor whiteColor];
    
    [self.readCollectionView registerClass:[LDY_ReadCollectionViewCell class] forCellWithReuseIdentifier:@"LDY_ReadCollectionViewCell"];
    
    /* 注册头尾视图 */
    [self.readCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LDY_ReadCollectionReusableView"];
    
    self.readCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    
    
    
    /* 轮播图 */
    self.cycleScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH * 375, KHEIGHT * 200)];
    [self.readCollectionView addSubview:self.cycleScrollView];
    self.cycleScrollView.delegate = self;
    self.cycleScrollView.placeholderImage = [UIImage imageNamed:@"wait"];

    self.cycleScrollView.pageControlDotSize = CGSizeMake(5, 5);
    self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    
    [self.cycleScrollView release];
    [flowLayout release];
    [self.readCollectionView release];

}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    LDY_CycleViewController *cycleViewController = [[LDY_CycleViewController alloc] init];
    
    cycleViewController.url = [self.cycleUrlArr objectAtIndex:index];
    
    [cycleScrollView release];
    
    [self.navigationController pushViewController:cycleViewController animated:YES];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LDY_ArticleDetailViewController *articleDetail = [[LDY_ArticleDetailViewController alloc] init];

    ReadingModel *model = [self.mainArr objectAtIndex:indexPath.row];
    
    articleDetail.type = [NSString stringWithFormat:@"%@", model.type];
    
    [self.navigationController pushViewController:articleDetail animated:YES];
    [articleDetail release];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.mainArr.count > 0) {
        return self.mainArr.count;
    }else{
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LDY_ReadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LDY_ReadCollectionViewCell" forIndexPath:indexPath];
    
    if (self.mainArr > 0) {
        cell.model = [self.mainArr objectAtIndex:indexPath.item];
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 9) {
        
        return CGSizeMake((KWIDTH * 375 - 20), (KWIDTH * 375 - 30) / 3);
    }else{
        
    return CGSizeMake((KWIDTH * 375 - 40) / 3, (KWIDTH * 375 - 40) / 3);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 10, 5, 10);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(KWIDTH * 375, KHEIGHT * 205);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
